package org.sasha.roulette.dialog;

import javax.inject.Inject;

import org.sasha.roulette.data.Spin;
import org.sasha.roulette.dialog.SpinForm.Callback;

import com.github.peholmst.mvp4vaadin.AbstractView;
import com.vaadin.Application;
import com.vaadin.ui.Form;
import com.vaadin.ui.Window;

@SuppressWarnings("serial")
public class SpinDialogViewImpl extends AbstractView<SpinDialogView, SpinDialogPresenter> implements SpinDialogView {

    private Window window = new Window() {{
        setWidth("300px");
        setHeight("180px");
        setCaption("Новый спин");
        setResizable(false);
        setModal(true);
        setClosable(false);
        center();  
    }};
    
    private Form content = new SpinForm(new Callback() {
        
        @Override
        public void save(final Spin spin) {
            getPresenter().saveSpin(spin);
            hide();
        }
        
        @Override
        public void cancel() {
            hide();
        }
    });
    
    private Application app;
    
    @Inject
    public SpinDialogViewImpl(final Application app, final SpinDialogPresenter presenter) {
        super();
        this.app = app;
        setPresenter(presenter);
        constructForm();
    }
    

    private void constructForm() {
        content.setSizeFull();
        window.addComponent(content);
    }

    @Override
    public void show() {
        app.getMainWindow().addWindow(window);
        content.setItemDataSource(getPresenter().createSpinItem(), getPresenter().getProperties());
    }

    @Override
    public void hide() {
        app.getMainWindow().removeWindow(window);
    }

}
